﻿@model XZen.Blogger.Models.RestCall
@{  
    Layout = null;
    var hideScript = true;
    var easyXDM = Model.MyDomain + "/Rpc/api.min.js";
    var jqueryUrl = Model.MyDomain + "/Scripts/jquery-1.7.1.min.js";
    var swf = Model.MyDomain + "/Rpc/easyxdm.swf";
    var remote = Model.MyDomain + "/Proxy/Index";
    var remoteHelper = Model.MyDomain + "/Rpc/name.html";
    
}
@*<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">*@

var RPC = {};
var edxUrl = "@easyXDM";
var jqueryUrl = "@jqueryUrl";
function loadScript(url, callback) {

    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState) {  //IE
        script.onreadystatechange = function () {
            if (script.readyState == "loaded" ||
                    script.readyState == "complete") {
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function () {
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}
RPC.Target = {};
RPC.Target.OnPost = function (message, origin) {
};
//RPC.Target.OnGet = function (message, origin) {
//};
RPC.Target.OnError = function (message, origin) {
};

RPC.ChannelType = function (type) {
};

RPC.ChannelType.Proxy = "proxy";
RPC.ChannelType.jQuery1 = "jquery1";
RPC.ChannelType.jQuery2 = "jquery2";
RPC.ChannelType.Cors = "CORS";


RPC.IsProxy = function () {
    return (RPC.Channel == RPC.ChannelType.Proxy);
};

RPC.Init = function (engine) {
    if (engine == "undefined" || engine == null) {
        RPC.Channel = RPC.ChannelType.Proxy;
    }
    else if (engine == "jquery") {
        RPC.Channel = RPC.ChannelType.jquery1;
    }
    else {
        RPC.Channel = RPC.ChannelType.Proxy;
    }
    //Call proxy init
    RPC.Target.Init();
}

RPC.Post = function(options,data, success,error){
throw " Post Not implimented";
};

RPC.Put = function(options,data, success,error){
throw "Put Not implimented";
};
RPC.Get = function(options,data, success,error){
throw "Get Not implimented";
};

RPC.Delete = function(options,data, success,error){
throw "Delete Not implimented";
};


RPC.Target.Init = function () {
    if (RPC.IsProxy()) {
        loadScript(edxUrl, function () {
            var transport = new easyXDM.Rpc({
                swf: "@swf",
                remote: "@remote",
                remoteHelper: "@remoteHelper"
            },
        {
            local: {
                returnResponse: function (response) {
                    RPC.Target.OnPost(response, edxUrl);
                },
                returnError: function (xhr, st, er) {                    
                    RPC.Target.OnError(xhr, er);
                }
            },
            remote: {
                Post: {},
                Get: {},
                noOp: {}
            }

        }
        );
            RPC.Target.Socket = transport;
        });
    }
    else {
        if (!jQuery) {
            loadScript(jqueryUrl, function () {
                RPC.Channel = RPC.ChannelType.jquery1;
            });
        }
    }
    RPC.Post = RPC.Target.Post;
    RPC.Get = RPC.Target.Get;
};

RPC.Target.Post = function (options, data, success, error) {
    if (RPC.IsProxy()) {
        RPC.Target.OnPost = function (message, origin) {
            if (success) {
                success(message, origin);
            }
        };
        RPC.Target.OnError = function (message, origin) {
            if (error) {
                error(message, origin);
            }
        };
        var args = {
            url: options.url,
            data: data,
            type: "POST",
            success: success,
            error: error
        };
        RPC.Target.Socket.Post(args);
    }
    else {
        //Dont know if it is loaded or not...
        $.support.cors = true;
        $.ajax({
            url:options.url,
            data:data,
            type:"POST",
              crossDomain:true,
            dataType:"json",
            success: function(d) { success(d); } ,
            error: function(x,e,s) { error(x,s); } 
        });
    }
};


RPC.Target.Get = function (options, data, success, error) {
    if (RPC.IsProxy()) {
        RPC.Target.OnPost = function (message, origin) {
            if (success) {
                success(message, origin);
            }
        };
        RPC.Target.OnGet = function (message, origin) {
            if (success) {
                success(message, origin);
            }
        };
        RPC.Target.OnError = function (message, origin) {
            if (error) {
                error(message, origin);
            }
        };
        var args = {
            url: options.url,
            data: data,
            type: "GET",
            success: success,
            error: error
        };
        RPC.Target.Socket.Get(args);
    }
    else {
        //Dont know if it is loaded or not...
          $.support.cors = true;
        $.ajax({
            url:options.url,
            data:data,
            type:"GET",
            crossDomain:true,
            dataType:"json",
            success: function(d) { success(d); } ,
            error: function(x,e,s) { error(x,s); } 
        });
    }
};

//Call
RPC.Init("proxy");

       
@*</script>*@
